package weekMatch.one_337;

import java.util.HashMap;
import java.util.Map;

public class LeeCode_02 {
    public static void main(String[] args) {

    }
    public boolean checkValidGrid(int[][] grid) {
        Map<Integer, int[]> map = new HashMap<>();
        int n = grid.length;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                map.put(grid[i][j], new int[]{i, j});
            }
        }
        if (map.get(0)[0] != 0 || map.get(0)[1] != 0)
            return false;
        for (int i = 1; i < n * n; i++) {
            int[] l1 = map.get(i - 1);
            int x1 = l1[0], y1 = l1[1];
            int[] l2 = map.get(i);
            int x2 = l2[0], y2 = l2[1];
            if (!((Math.abs(x1 - x2) == 1 && Math.abs(y1 - y2) == 2) ||
                (Math.abs(x1 - x2) == 2 && Math.abs(y1 - y2) == 1))){
                return false;
            }
        }
        return true;
    }
}
